/**
 * 菜单JS
 */
angular.module('mall.menu', [
        "ngStorage"
    ])

    .config(['$stateProvider', '$urlRouterProvider', function ($stateProvider, $urlRouterProvider) {
        $stateProvider
        //店铺列表页
            .state('index', {
                url: '/s?menus&county&orderid&bytag2&shopid&deshopid&goodsid&Paystate&Shopenter',
                controller: 'IndexController'
            })
            //县镇店铺列表页
            .state('county', {
                url: '/county/:id?name',
                templateUrl: templateprefix + 'menu/county.html',
                resolve: {
                    VillageList: ['$stateParams', 'ShopService', function ($stateParams, ShopService) {
                        return ShopService.selectAllCountyShops($stateParams.id);
                    }]
                },
                controller: 'CountyController'
            })
            //标签店铺列表页
            .state('bytag', {
                url: '/bytag2/:id?name',
                templateUrl: templateprefix + 'menu/bytag2.html',
                controller: 'MaterialController'
            })

            //标签店铺列表页
            .state('mall.bytag', {
                url: '/bytag/:id?name',
                templateUrl: templateprefix + 'menu/bytag.html',
                resolve: {
                    shoplist: ['$stateParams', 'ShopService', function ($stateParams, ShopService) {
                        return ShopService.queryShoplistByGroup($stateParams.id).$promise;
                    }]
                },
                controller: 'ByTagController'
            })
        ;
    }])

    //农产品店铺列表
    .controller('IndexController', ['$scope', '$state', '$stateParams', '$localStorage',
        function ($scope, $state, $stateParams, $localStorage) {
            if ($stateParams.menus) {
                $state.go('mall.bytag', {id: $stateParams.menus});
            } else if ($stateParams.county) {
                $state.go('county', {id: $stateParams.county});
            } else if ($stateParams.orderid) {
                $state.go('placeorder', {orderid: $stateParams.orderid, Paystate: $stateParams.Paystate});
            } else if ($stateParams.bytag2) {
                $state.go('bytag', {id: $stateParams.bytag2});
            } else if ($stateParams.shopid) {
                $state.go('goods.goods', {shopid: $stateParams.shopid, Shopenter: $stateParams.Shopenter})
                /*var json={time:new Date().getTime()};
                 window.history.pushState(json," ","http://www.nongui8/mobile/");*/
            }
        }])

    //农产品店铺列表
    .controller('CountyController', ['$scope', '$state', '$stateParams', 'VillageList',
        function ($scope, $state, $stateParams, VillageList) {
            $scope.mall_enturl = projectBaseUrl;//回跳地址
            hidename()

            $scope.countyid = $stateParams.id;
            if (!$stateParams.name) {
                $scope.countyname = '泰顺县'
            }
            else {
                $scope.countyname = $stateParams.name;
            }
            $scope.villagelist = VillageList;
            $scope.renderFinish = function () {
                $("#left li").eq(0).addClass("active");
                $("#right .home").eq(0).addClass("active");
                new transformScrolls("left_ul", "shop-list");
            };

            $scope.inner = function (shopid) {//页面跳转
                $state.go("goods.goods", {shopid: shopid});
            }
            $scope.changeCounty = function (countyid, $event) {
                if (!countyid) {
                    $(".djshow").fadeIn();
                }
                else {
                    $(".djshow").fadeOut();
                    $scope.name = $event.target.innerHTML
                    $state.go('county', {id: countyid, name: $scope.name});
                }
            }
        }])

    //农资店铺列表
    .controller('MaterialController', ['$scope', '$state', '$stateParams', 'ShopService',
        function ($scope, $state, $stateParams, ShopService) {
            $scope.mall_enturl = projectBaseUrl;//回跳地址

            var tagid = 10;
            if ($stateParams.id) {
                tagid = $stateParams.id
            }
            $scope.shoplist = ShopService.queryShoplistByGroup(tagid);
            //自定义数组测试评分对应星星显示
            $scope.stores = [{name: '国产金坷垃', score: '4.8', star: ""},
                {name: '化肥店', score: '4.2', star: ""},
                {name: '各种蔬菜种子', score: '3.8', star: ""},
                {name: '你想要锄头吗？', score: '3', star: ""},
                {name: '你想要锄头吗？', score: '0.1', star: ""},
                {name: '你想要锄头吗？', score: '0', star: ""}];

            for (var i = 0; i < $scope.stores.length; i++) {
                var score = $scope.stores[i].score
                if (score <= 5 && score >= 4.8) {
                    $scope.stores[i].star = 'star_bac_0'
                }
                else if (score < 4.8 && score >= 4.2) {
                    $scope.stores[i].star = 'star_bac_1'
                } else if (score < 4.2 && score >= 3.8) {
                    $scope.stores[i].star = 'star_bac_2'
                }
                else if (score < 3.8 && score >= 3.2) {
                    $scope.stores[i].star = 'star_bac_3'
                }
                else if (score < 3.2 && score >= 2.8) {
                    $scope.stores[i].star = 'star_bac_4'
                }
                else if (score < 2.8 && score >= 2.2) {
                    $scope.stores[i].star = 'star_bac_5'
                }
                else if (score < 2.2 && score >= 1.8) {
                    $scope.stores[i].star = 'star_bac_6'
                }
                else if (score < 1.8 && score >= 1.2) {
                    $scope.stores[i].star = 'star_bac_7'
                }
                else if (score < 1.2 && score >= 0.8) {
                    $scope.stores[i].star = 'star_bac_8'
                }
                else if (score < 0.8 && score >= 0.2) {
                    $scope.stores[i].star = 'star_bac_9'
                }
                else {
                    $scope.stores[i].star = 'star_bac_10'
                }
            }
        }])

    //餐谋团商城店铺列表
    .controller('ByTagController', ['$scope', '$state', '$stateParams', '$localStorage', 'shoplist', function ($scope, $state, $stateParams, $localStorage, shoplist) {
        var $storage = $localStorage.$default({
            name: '',
            userId: '',
            wxpay_openid: '',
            shopcart: {},
        });
        opacity_top();
        $scope.id = $stateParams.id;
        $scope.tagname = $stateParams.name;
        $scope.shoplist = shoplist;
    }])
;
